﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using Urd.Packet;
using UnityEngine;
//using Urd.Common;

namespace Wernandy.Posclient
{
    public class posgetter:
        IDisposable
    {
        private pospack current;
        public List<int> ID_List { get { return current.IDlist; } }
        //public pospack Current { get { return current; } }
        private connect_sequience c;
        private Thread worker;
        private bool working;
        public bool Working { get { return working; } }
        public Queue<sendinfo> s;
        private void work()
        {
            while(working)
            {
                try
                {
                    if (c.Recieved)
                    {
                        if(c.s.recieved.command == Command.GetOK)
                        current = new pospack(c.s.recieved,c.s.lag);
                        
                    }
                    if (c.Ready)
                    {

                        if (s.Count>0&&s.Peek() != null)
                        {
                            c.Connect(s.Dequeue());
                        }
                        else 
                        {
                            c.Connect(new sendinfo(new packet(Opcode.Pos,Command.Get)));
                        }
                    }
                }
                catch (Exception e)
                {
                    //UnityEngine.Debug.Log(e.Message); 
                }
                finally { }
                //Thread.Sleep(10);
            }
        }

        public posgetter(IPAddress ip, int port)
        {
            working = false;
            current = new pospack();
            s = new Queue<sendinfo>();
            //s.tosend = new packet(Opcode.Pos);
            worker = new Thread(new ThreadStart(work));
            c = new connect_sequience(ip, port, "pos");
            working = true;
            worker.Start();
        }
        

        public void Dispose()
        {
            
            working = false;
            worker.Abort();
        }
        public Vector3 Take(int id) 
        {
            pos  p = current.Take(id);
            return new Vector3(p.X, p.Y, p.Z);
        }
        public void Put(Vector3 position, int id,Command c = Command.Set) 
        {
            packet p = new packet(Opcode.Pos, c);
            p.Push(id);
            p.Push(position.x);
            p.Push(position.y);
            p.Push(position.z);
            if(s.Count<20)
            s.Enqueue(new sendinfo(p));

        }
        
    }
    public class posgetterr :
       IDisposable
    {
        private pospackr current;
        public List<int> ID_List { get { return current.IDlist; } }
        //public pospack Current { get { return current; } }
        private connect_sequience c;
        private Thread worker;
        private bool working;
        public bool Working { get { return working; } }
        public Queue<sendinfo> s;
        private void work()
        {
            while (working)
            {
                try
                {
                    if (c.Recieved)
                    {
                        if (c.s.recieved.command == Command.GetOK)
                            current = new pospackr(c.s.recieved, c.s.lag);

                    }
                    if (c.Ready)
                    {

                        if (s.Count > 0 && s.Peek() != null)
                        {
                            c.Connect(s.Dequeue());
                        }
                        else
                        {
                            c.Connect(new sendinfo(new packet(Opcode.Pos, Command.Get)));
                        }
                    }
                }
                catch (Exception e)
                {
                    //UnityEngine.Debug.Log(e.Message); 
                }
                finally { }
                //Thread.Sleep(10);
            }
        }

        public posgetterr(IPAddress ip, int port)
        {
            working = false;
            current = new pospackr();
            s = new Queue<sendinfo>();
            //s.tosend = new packet(Opcode.Pos);
            worker = new Thread(new ThreadStart(work));
            c = new connect_sequience(ip, port, "pos");
            working = true;
            worker.Start();
        }


        public void Dispose()
        {

            working = false;
            worker.Abort();
        }
        public XYZR Take(int id)
        {
            XYZR p = current.Take(id);
            return p;
        }
        public void Put(Vector3 position, float roty, int id, Command c = Command.Set)
        {
            packet p = new packet(Opcode.Pos, c);
            p.Push(id);
            p.Push(position.x);
            p.Push(position.y);
            p.Push(position.z);
            p.Push(roty);
            if (s.Count < 20)
                s.Enqueue(new sendinfo(p));

        }

    }
}
